package com.bbc.yuntun.platform.controller.wx;

import com.bbc.yuntun.common.core.controller.BaseController;
import com.bbc.yuntun.common.core.domain.AjaxResult;
import com.bbc.yuntun.common.core.domain.R;
import com.bbc.yuntun.common.utils.ServletUtils;
import com.bbc.yuntun.common.utils.StringUtils;
import com.bbc.yuntun.platform.domain.model.WxLoginUser;
import com.bbc.yuntun.platform.domain.WxUser;
import com.bbc.yuntun.platform.domain.model.WxLoginBody;
import com.bbc.yuntun.platform.service.admin.WxUserService;
import com.bbc.yuntun.platform.service.wx.WxLoginService;
import com.bbc.yuntun.platform.service.wx.WxTokenService;
import com.bbc.yuntun.platform.utils.WxUserUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * 微信授权
 */
@Tag(name = "微信授权")
@RestController
@RequiredArgsConstructor
@RequestMapping("/wx/auth")
public class WxAuthController extends BaseController {

    private final WxLoginService wxLoginService;

    private final WxTokenService wxTokenService;

    private final WxUserService wxUserService;

    /**
     * 微信登录
     */
    @Operation(summary = "微信登录")
    @PostMapping("/login")
    public AjaxResult wxUserLogin(@RequestBody WxLoginBody loginBody) throws Exception {
        String token = wxLoginService.wxUserLogin(loginBody);
        WxUser wxUser = wxUserService.getById(wxTokenService.getLoginUser(token).getWxUser().getId());
        return AjaxResult.success(new HashMap<String, Object>(2) {{
            put("token", token);
            put("wxUser", wxUser);
        }});
    }

    /**
     * 微信测试
     */
    @Operation(summary = "微信测试")
    @GetMapping("/test")
    public AjaxResult test() {
        WxLoginUser wxLoginUser = new WxLoginUser();
        wxLoginUser.setWxUser(wxUserService.getById(1));
        String token = wxTokenService.createToken(wxLoginUser);
        return AjaxResult.success(new HashMap<String, Object>(2) {{
            put("token", token);
        }});
    }

    /**
     * 获取当前微信用户
     */
    @Operation(summary = "获取当前微信用户")
    @GetMapping("/getInfo")
    public AjaxResult getInfo() {
        return AjaxResult.success(wxUserService.getById(WxUserUtils.getWxUserId()));
    }

    /**
     * 微信用户信息更新
     */
    @Operation(summary = "微信用户信息更新")
    @PutMapping("/updateInfo")
    public R<WxUser> updateInfo(@RequestBody WxUser updateInfo) {
        WxLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
        WxUser wxUser = loginUser.getWxUser();
        // 主要更新真实姓名、性别、头像、昵称以及 手机号等
        wxUser.setTrueName(updateInfo.getTrueName());
        wxUser.setSex(updateInfo.getSex());
        String avatarUrl = updateInfo.getAvatarUrl();
        if(StringUtils.isNotEmpty(avatarUrl)) {
            wxUser.setAvatarUrl(avatarUrl);
        }
        String nickName = updateInfo.getNickName();
        if(StringUtils.isNotEmpty(nickName)) {
            wxUser.setNickName(nickName);
        }
        String mobile = updateInfo.getMobile();
        if(StringUtils.isEmpty(mobile)) {
            wxUser.setMobile(mobile);
        }
        wxUserService.updateById(updateInfo);
        wxTokenService.setLoginUser(loginUser);
        return R.ok(updateInfo);
    }

    /**
     * 退出登录
     */
    @Operation(summary = "退出登录")
    @PostMapping("/logout")
    public AjaxResult logout(HttpServletRequest request) {
        WxLoginUser wxLoginUser = wxTokenService.getLoginUser(request);
        if (StringUtils.isNotNull(wxLoginUser)) {
            wxTokenService.delLoginUser(wxLoginUser.getToken());
        }
        return AjaxResult.success();
    }

}
